package com.ns.scs.task.excutor;

import com.ns.core.current.config.SymbolsMark;
import com.ns.core.utils.bean.ServiceUtil;
import com.ns.core.utils.sg.ResultError;
import com.ns.scs.ads.service.IAdsService;
import com.ns.scs.task.excutor.basic.ScheduledBasicActuator;
import com.ns.scs.task.init.TaskValidUtils;
import com.ns.scs.task.mapper.ScheduledTaskMapper;
import com.ns.scs.task.service.IScheduledTaskLogsService;
import com.ns.scs.task.vo.ScheduledTaskVO;
import lombok.extern.slf4j.Slf4j;


/**
 * 广告过期执行器
 * <p>
 *
 * @author liao.wei
 * @since 2025/01/08
 */
@Slf4j
public class ScheduledAdsExpirationActuator extends ScheduledBasicActuator {
    public ScheduledAdsExpirationActuator(ScheduledTaskVO scheduledTask) {
        super(scheduledTask);
    }

    @Override
    public void run() {
        ScheduledTaskVO scheduledTask = super.getScheduledTask();
        IScheduledTaskLogsService scheduledTaskLogsService = ServiceUtil.getBean(IScheduledTaskLogsService.class);
        try {
            // 校验任务和执行器状态
            if (!TaskValidUtils.isTaskValid(scheduledTask)) {
                return;
            }
            // 更新任务状态
            ScheduledTaskMapper taskMapper = ServiceUtil.getBean(ScheduledTaskMapper.class);
            taskMapper.updateExecStatus(SymbolsMark.ONE, scheduledTask.getId());
            // 同步访问量
            IAdsService adsService = ServiceUtil.getBean(IAdsService.class);
            adsService.syncExpiration();
            // 更新任务状态
            taskMapper.updateExecStatus(SymbolsMark.TWO, scheduledTask.getId());
            // 记录日志
            scheduledTaskLogsService.saveLogs(scheduledTask, SymbolsMark.TWO, null);
        } catch (Exception e) {
            throw new ResultError(scheduledTaskLogsService.saveLogs(scheduledTask, SymbolsMark.NEG_1, e));
        }
    }
}